using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using Weazel.Toad.TextEditor.Document;

namespace Weazel.Toad.TextEditor
{
	/// <summary>
	/// Contains brushes/pens for the text editor to speed up drawing. Re-Creation of brushes and pens
	/// seems too costly.
	/// </summary>
	public class BrushRegistry
	{
		static Dictionary<Color, Brush> brushes = new Dictionary<Color, Brush>();
		static Dictionary<Color, Pen> pens    = new Dictionary<Color,Pen>();
		static Dictionary<Color, Dictionary<Color, Pen>> dotPens = new Dictionary<Color,Dictionary<Color,Pen>>();
		
		public static Brush GetBrush(Color color)
		{
			if (!brushes.ContainsKey(color)) {
				Brush newBrush = new SolidBrush(color);
				brushes.Add(color, newBrush);
				return newBrush;
			}
			return brushes[color];
		}
		
		public static Pen GetPen(Color color)
		{
			if (!pens.ContainsKey(color)) {
				Pen newPen = new Pen(color);
				pens.Add(color, newPen);
				return newPen;
			}
			return pens[color];
		}
		
		public static Pen GetDotPen(Color bgColor, Color fgColor)
		{
			bool containsBgColor = dotPens.ContainsKey(bgColor);
			if (!containsBgColor || !dotPens[bgColor].ContainsKey(fgColor)) {
				if (!containsBgColor) {
					dotPens[bgColor] = new Dictionary<Color,Pen>();
				}
				
				HatchBrush hb = new HatchBrush(HatchStyle.Percent50, bgColor, fgColor);
				Pen newPen = new Pen(hb);
				dotPens[bgColor][fgColor] = newPen;
				return newPen;
			}
			return dotPens[bgColor][fgColor];
		}
	}
}
